package com.lishe.lishe.lishe.module.excitation.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.base.common.base.BaseFragment;
import com.lishe.lishe.lishe.bean.SpaceItemDecoration;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.excitation.activity.PersonAchievementActivity;
import com.lishe.lishe.lishe.module.excitation.activity.PersonDianZanActivity;
import com.lishe.lishe.lishe.module.excitation.activity.PersonGiftActivity;
import com.lishe.lishe.lishe.module.excitation.activity.PersonImpressionActivity;
import com.lishe.lishe.lishe.module.excitation.adapter.MineExcitationListAdapter;
import com.lishe.lishe.lishe.module.excitation.bean.MineExcitationObject;
import com.lishe.lishe.lishe.module.excitation.bean.PersonInfoObject;
import com.lishe.lishe.lishe.module.excitation.presenter.MinePresenter;
import com.lishe.lishe.lishe.utils.SharedPreferencesUtils;
import com.lishe.lishe.lishe.utils.Utils;
import com.lishe.lishe.lishe.widget.CircleImageView;
import com.lishe.lishe.retrofit.BaseDataPageBody;
import com.lishe.lishe.retrofit.DataResponseListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shino on 2017/10/21.
 * 我的激励
 */

public class MineExcitationFragment extends BaseFragment implements View.OnClickListener {

    private static final String TAG = MineExcitationFragment.class.getSimpleName();

    private RecyclerView mAchievementList, mGiftList;
    private ImageView mBack;
    private CircleImageView mHead;
    private TextView mName, mJob, mOtherZan, mZan, mReceiveZan, mPersonImpression, mTextAchievement, mTextGift, mEmptyText1, mEmptyText2;
    private LinearLayout mAchievement, mGift, mLiearOtherZan, mLiearZan, mLiearReceiveZan;
    private View view;

    private static ArrayList<MineExcitationObject> mAList, mGList;
    private MineExcitationListAdapter mAchievementAdaper, mGiftAdapter;
    private String userId = null;
    private PersonInfoObject mPersonInfo;
    private int num=4;

    public static MineExcitationFragment getInstance(String userId) {
        MineExcitationFragment fragment = new MineExcitationFragment();
        Bundle bundle = new Bundle();
        bundle.putString("userId", userId);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.activity_mine_excitation, null);
            if (getArguments() != null) {
                userId = getArguments().getString("userId");
            }
            initView(view);
            setListener();
            initAdapter();
        }
        getPersonInfo();
        view.setOnTouchListener(this);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1001) {
            if (resultCode == 0) {
                if (data.getBooleanExtra("dataIsChange", false)) {
                    mGList.clear();
                    mGList.addAll((ArrayList<MineExcitationObject>) data.getSerializableExtra("list"));
                    mGiftAdapter.notifyDataSetChanged();
                    if (mGList.size() == 0 && mEmptyText2!=null)
                        mEmptyText2.setVisibility(View.VISIBLE);
                }
            }
        }

    }

    private void initView(View view) {
        mAchievementList = view.findViewById(R.id.achievement_list);
        mGiftList = view.findViewById(R.id.gift_list);
        mBack = view.findViewById(R.id.back_title);
        mHead = view.findViewById(R.id.head_icon);
        mName = view.findViewById(R.id.name);
        mJob = view.findViewById(R.id.job);
        mOtherZan = view.findViewById(R.id.other_zan_num);
        mZan = view.findViewById(R.id.zan_num);
        mReceiveZan = view.findViewById(R.id.receive_zan_num);
        mAchievement = view.findViewById(R.id.achievement);
        mGift = view.findViewById(R.id.gift);
        mPersonImpression = view.findViewById(R.id.person_impression);
        mLiearOtherZan = view.findViewById(R.id.ll_other_zan);
        mLiearZan = view.findViewById(R.id.ll_zan);
        mLiearReceiveZan = view.findViewById(R.id.ll_receive_zan);
        mTextAchievement = view.findViewById(R.id.text_achievement);
        mTextGift = view.findViewById(R.id.text_gift);
        mEmptyText1 = view.findViewById(R.id.empty_text_1);
        mEmptyText2 = view.findViewById(R.id.empty_text_2);

        mAchievementList.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        mGiftList.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        mAchievementList.addItemDecoration(new SpaceItemDecoration(0, Utils.dip2px(getActivity(), 10), 0, 0));
        mGiftList.addItemDecoration(new SpaceItemDecoration(0, Utils.dip2px(getActivity(), 10), 0, 0));

        if (TextUtils.isEmpty(userId)) {
            mTextAchievement.setText(R.string.my_achievement);
            mTextGift.setText(R.string.my_gift);
        } else {
            mTextAchievement.setText(R.string.ta_achievement);
            mTextGift.setText(R.string.ta_gift);
        }

    }

    private void setListener() {
        mAchievement.setOnClickListener(this);
        mGift.setOnClickListener(this);
        mPersonImpression.setOnClickListener(this);
        mBack.setOnClickListener(this);
//        mLiearReceiveZan.setOnClickListener(this);
        mLiearZan.setOnClickListener(this);
        mLiearOtherZan.setOnClickListener(this);
        mHead.setOnClickListener(this);
    }

    private void initAdapter() {
        mAList = new ArrayList<>();
        mGList = new ArrayList<>();
        mAchievementAdaper = new MineExcitationListAdapter(getActivity(), mAList);
        mAchievementAdaper.setOnItemClickListener(new RecyclerviewBaseAdapter.ItemListener.ItemClickListener() {
            @Override
            public void OnClickListener(RecyclerView.Adapter adapter, View view, int position) {
                PersonAchievementActivity.jump(getActivity(), userId);
            }
        });
        mGiftAdapter = new MineExcitationListAdapter(getActivity(), mGList);
        mGiftAdapter.setOnItemClickListener(new RecyclerviewBaseAdapter.ItemListener.ItemClickListener() {
            @Override
            public void OnClickListener(RecyclerView.Adapter adapter, View view, int position) {
                PersonGiftActivity.jump(MineExcitationFragment.this, userId);
            }
        });
        mAchievementList.setAdapter(mAchievementAdaper);
        mGiftList.setAdapter(mGiftAdapter);
    }

    /**
     * 获取个人信息
     */
    public void getPersonInfo() {
        try {
            MinePresenter.getInstance().getUserInfo(Globals.getToken(), userId, new DataResponseListener<PersonInfoObject>() {
                @Override
                public void onSuccess(PersonInfoObject info) {
                    if (!MineExcitationFragment.this.isAdded()) {
                        return;
                    }
                    updatePersonInfo(info);
                    SharedPreferencesUtils.saveExcitationPersonInfo(getActivity(), info.getAvatarPath(), info.getEmpName());

                    //获取用户信息成功后在获取成就和小礼
                    getAchievement();
                    getGift();
                }

                @Override
                public void onFail(String failMessage) {
                    toast(failMessage);
                }

                @Override
                public void onError(Throwable e) {
                    toast(R.string.network_or_server_exception);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            toast(getString(R.string.data_parse_fail));
        }

    }

    /**
     * 获取个人小礼列表
     */
    public void getGift() {
        if (mEmptyText2.getVisibility() == View.VISIBLE) {
            mEmptyText2.setVisibility(View.GONE);
        }
        try {
            MinePresenter.getInstance().getUserGifts(Globals.getToken(), userId, 1, 10,num, new DataResponseListener<BaseDataPageBody<MineExcitationObject>>() {
                @Override
                public void onSuccess(BaseDataPageBody<MineExcitationObject> dataResponse) {
                    mGList.clear();
                    mGList.addAll(dataResponse.getInfo());
                    mGiftAdapter.notifyDataSetChanged();

                    if (mGList.size() == 0) mEmptyText2.setVisibility(View.VISIBLE);
                }

                @Override
                public void onFail(String failMessage) {
                    if (mGList.size() == 0) mEmptyText2.setVisibility(View.VISIBLE);
                    toast(failMessage);
                }

                @Override
                public void onError(Throwable e) {
                    if (mGList.size() == 0) mEmptyText2.setVisibility(View.VISIBLE);
                    toast(R.string.network_or_server_exception);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            toast(getString(R.string.data_parse_fail));
        }

    }

    /**
     * 获取个人成就列表
     */
    public void getAchievement() {
        if (mEmptyText1.getVisibility() == View.VISIBLE) {
            mEmptyText1.setVisibility(View.GONE);
        }
        try {
            MinePresenter.getInstance().getUserAchieveList(Globals.getToken(), userId,num, new DataResponseListener<List<MineExcitationObject>>() {
                @Override
                public void onSuccess(List<MineExcitationObject> dataResponse) {
                    mAList.clear();
                    mAList.addAll(dataResponse);
                    mAchievementAdaper.notifyDataSetChanged();
                    if (mAList.size() == 0) {
                        mEmptyText1.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onFail(String failMessage) {
                    if (mAList.size() == 0) mEmptyText1.setVisibility(View.VISIBLE);
                    toast(failMessage);
                }

                @Override
                public void onError(Throwable e) {
                    if (mAList.size() == 0) mEmptyText1.setVisibility(View.VISIBLE);
                    toast(R.string.network_or_server_exception);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            if (mAList.size() == 0) mEmptyText1.setVisibility(View.VISIBLE);
            toast(getString(R.string.data_parse_fail));
        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

    }

    private void back() {
        getActivity().finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.person_impression:
                if (mPersonInfo != null)
                    PersonImpressionActivity.jump(getActivity(), userId, mPersonInfo.getEmpName(), mPersonInfo.getAvatarPath());
                break;
            case R.id.achievement:
                PersonAchievementActivity.jump(getActivity(), userId);
                break;
            case R.id.gift:
                PersonGiftActivity.jump(MineExcitationFragment.this, userId);
                break;
            case R.id.back_title:
                back();
                break;
            case R.id.ll_other_zan:
                PersonDianZanActivity.jump(getActivity(), PersonDianZanActivity.BEIZAN, userId);
                break;
            case R.id.ll_zan:
                PersonDianZanActivity.jump(getActivity(), PersonDianZanActivity.DIANZAN, userId);
                break;
            case R.id.ll_receive_zan:
                PersonDianZanActivity.jump(getActivity(), PersonDianZanActivity.BEIZAN, userId);
                break;
            case R.id.head_icon:
                if (mName.getText().toString().trim().equals("未登录")) {
                    getPersonInfo();
                    getAchievement();
                    getGift();
                }

                break;
        }
    }

    private void updatePersonInfo(PersonInfoObject info) {
        mPersonInfo = info;
        mName.setText(info.getEmpName());
        if (!TextUtils.isEmpty(info.getPosName())) {
            mJob.setText(info.getPosName());
        }
        mOtherZan.setText(String.valueOf(info.getBeLaudCount()));
        mZan.setText(String.valueOf(info.getLaudCount()));
        mReceiveZan.setText(String.valueOf(info.getAdmireCount()));
        ImageLoaderUtils.displayCircleImage(this, info.getAvatarPath(), mHead,R.mipmap.head);
    }

}
